package com.bhkj.pdjhbgnewproject.core.base;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.bhkj.pdjhbgnewproject.common.callback.CallbackManager;
import com.bhkj.pdjhbgnewproject.core.notlogin.NotLoginActivity;
import com.blankj.utilcode.util.NetworkUtils;

public abstract class BaseActivity extends AppCompatActivity implements NetworkUtils.OnNetworkStatusChangedListener {


    private AlertDialog mAlertDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        screenFull();
        super.onCreate(savedInstanceState);
        if (getLayoutId() > 0) {
            setContentView(getLayoutId());
            initView();
            initData();
        } else {
            throw new RuntimeException("layout id must >0");
        }

        NetworkUtils.registerNetworkStatusChangedListener(this);
    }


    @Override
    public void onDisconnected() {
        showNotNetTip();
    }

    @Override
    public void onConnected(NetworkUtils.NetworkType networkType) {
        dissNotNetTip();
        if (this instanceof NotLoginActivity) {
            obtainWindowInfo();
        }
    }

    public void obtainWindowInfo(){

    };


    private void showNotNetTip() {
        if (mAlertDialog == null) {
            mAlertDialog = new AlertDialog.Builder(this)
                    .setMessage("当前设备网络不可用")
                    .setCancelable(false)
                    .create();
        }
        mAlertDialog.show();
    }


    private void dissNotNetTip() {
        if (mAlertDialog != null&&mAlertDialog.isShowing()) {
            mAlertDialog.dismiss();
        }
    }


    public abstract int getLayoutId();

    public abstract void initView();

    public abstract void initData();


    /**
     * 全屏展示
     */
    private void screenFull() {
        Window window = getWindow();
        //隐藏屏幕底部虚拟按键
        WindowManager.LayoutParams params = window.getAttributes();
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
        window.setAttributes(params);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        NetworkUtils.unregisterNetworkStatusChangedListener(this);
    }

}
